# -*- coding: UTF-8 -*-
# @Time : 2022-01-12 21:50
# @File : Thread.py
import random
import threading
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from logs.LogUtil import my_log


class myThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.log = my_log('日志')

    def run(self):
        print('开始线程')
        driver = webdriver.Firefox(executable_path="D:\geckodriver.exe")
        driver.get("http://m-uat.loyalfriends.cn")
        driver.set_window_size(460, 900)
        time.sleep(5)
        # 滚动到底部
        driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        # 点击新车未上牌，立即报价
        time.sleep(1)
        quote = By.XPATH, "//*[text()='新车未上牌，立即报价']"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(quote))
        ele_quote = driver.find_element(*quote)
        ActionChains(driver).move_to_element(ele_quote).click(ele_quote).perform()

        # 点击
        notification = By.XPATH, "//*[text()='《客户告知书》']"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification))
        ele_notification = driver.find_element(*notification)
        ActionChains(driver).move_to_element(ele_notification).click(ele_notification).perform()

        time.sleep(1)
        # 页面滚动条拖到底部
        # 页面滚动条拖到底部
        ScrollBox = By.XPATH, '//*[@id="scrollDiv"]'
        # WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(ScrollBox))
        ele_ScrollBox = driver.find_element(*ScrollBox)
        driver.execute_script("arguments[0].scrollIntoView();", ele_ScrollBox)
        time.sleep(2)
        js1 = 'document.getElementById("scrollDiv").scrollTop=10000'
        driver.execute_script(js1)
        time.sleep(2)

        know = By.XPATH, "/html/body/div/div/div[8]/div/div[2]/div[2]/button/div/span"
        # WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(know))
        ele_know = driver.find_element(*know)
        ActionChains(driver).move_to_element(ele_know).click(ele_know).perform()
        notification_know = By.XPATH, "/html/body/div/div/div[8]/div/div[2]/div[2]/button/div/span"

        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        # 隐私政策滚动
        ScrollBox = By.XPATH, '//*[@id="scrollDiv"]'
        # WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(ScrollBox))
        ele_ScrollBox = driver.find_element(*ScrollBox)
        driver.execute_script("arguments[0].scrollIntoView();", ele_ScrollBox)
        time.sleep(2)
        js1 = 'document.getElementById("scrollDiv").scrollTop=10000'
        driver.execute_script(js1)
        time.sleep(2)

        # 我知道了
        notification_know = By.XPATH, "/html/body/div/div/div[8]/div/div[2]/div[2]/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        # 我知道了
        notification_know = By.XPATH, "//*[text()='我知道了']"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()
        print('退出线程')


        js = "var q=document.documentElement.scrollTop=0"
        driver.execute_script(js)

        notification_know = By.XPATH, "//*[@placeholder='请选择投保城市']"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        notification_know = By.XPATH, "//*[@class='van-picker__confirm']"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[1]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "单子枫").perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[3]/div[" \
                                      "2]/div[1]/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "411123199602289613").perform()

        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[6]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/div[2]/div/div[1]/button[2]"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[7]/div[" \
                                      "3]/div/div/i "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[10]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "13513856271").perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[11]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "527286551@qq.com").perform()

        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[12]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/div[4]/div/div/div[" \
                                      "1]/button[2] "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]/div/div/div/form/div/div[13]/div/div[" \
                                      "1]/textarea "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "XXXXXXXXXXXXXXXXX").perform()

        # 向下滑动400
        js = "var q=document.documentElement.scrollTop=400"
        driver.execute_script(js)


        # 车架号
        # 随机生成6位数
        str = ""
        for i in range(6):
            ch = chr(random.randrange(ord('0'), ord('9') + 1))
            str += ch

        print(str)
        strs = "DHJSKYDT7HJ"+str
        self.log.debug('Thread========车架号：'+strs)
        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[2]/div[2]/div/div/div/form/div/div[2]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            strs).perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[2]/div[2]/div/div/div/form/div/div[3]/div[" \
                                      "2]/div/input "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "DH" + str).perform()

        time.sleep(5)
        notification_know = By.XPATH, "//*[@id='carinfobox']/div/form/div/div[4]/div[2]/div/input"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).send_keys(
            "长城CC6470轿车").perform()

        time.sleep(5)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[2]/div[2]/div/div/div/form/div/div[" \
                                      "4]/button/div/span "
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[2]/div[2]/div/div/div/div[2]/div/div[1]/button[2]"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div[1]/div[2]"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/form/div[1]/div[2]/div[1]/div[2]/div/div"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/form/div[1]/div[2]/div[1]/div[2]/div"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/form/div[2]/div/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(20)
        notification_know = By.XPATH, "/html/body/div/div/div[5]/div[2]/div/div[3]/button"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[4]/div/button[2]/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[6]/div[2]/div[2]/div[1]/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(1)
        # 页面滚动条拖到底部
        # 页面滚动条拖到底部
        ScrollBox = By.XPATH, '//*[@id="scrollDiv"]'
        # WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(ScrollBox))
        ele_ScrollBox = driver.find_element(*ScrollBox)
        driver.execute_script("arguments[0].scrollIntoView();", ele_ScrollBox)
        time.sleep(2)
        js1 = 'document.getElementById("scrollDiv").scrollTop=50000'
        driver.execute_script(js1)
        time.sleep(2)

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[8]/div/div/div[2]/div[2]/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[6]/div[2]/div[2]/div[1]/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(1)
        # 页面滚动条拖到底部
        # 页面滚动条拖到底部
        ScrollBox = By.XPATH, '//*[@id="scrollDiv"]'
        # WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(ScrollBox))
        ele_ScrollBox = driver.find_element(*ScrollBox)
        driver.execute_script("arguments[0].scrollIntoView();", ele_ScrollBox)
        time.sleep(2)
        js1 = 'document.getElementById("scrollDiv").scrollTop=10000'
        driver.execute_script(js1)
        time.sleep(2)

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[8]/div/div/div[2]/div[2]/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        time.sleep(2)
        notification_know = By.XPATH, "/html/body/div/div/div[6]/div[2]/div[2]/div[1]/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

        notification_know = By.XPATH, "/html/body/div/div/div[6]/div[2]/div[3]/button/div/span"
        WebDriverWait(driver, 20, 3).until(EC.presence_of_element_located(notification_know))
        ele_notification_know = driver.find_element(*notification_know)
        ActionChains(driver).move_to_element(ele_notification_know).click(ele_notification_know).perform()

def main():
    threads = []
    size = range(5)
    for i in size:
        thread = myThread()
        threads.append(thread)
    for i in size:
        threads[i].start()
    for i in size:
        threads[i].join()


if __name__ == '__main__':
    main()
